﻿using System;
using System.Text;

namespace HslCommunication.Core.Address
{
	/// <summary>
	/// Modbus协议地址格式，可以携带站号，功能码，地址信息<br />
	/// Modbus protocol address format, can carry station number, function code, address information
	/// </summary>
	// Token: 0x0200016D RID: 365
	public class ModbusAddress : DeviceAddressBase
	{
		/// <summary>
		/// 实例化一个默认的对象<br />
		/// Instantiate a default object
		/// </summary>
		// Token: 0x06001E0A RID: 7690 RVA: 0x000904A4 File Offset: 0x0008E6A4
		public ModbusAddress()
		{
			this.Station = -1;
			this.Function = -1;
			base.Address = 0;
		}

		/// <summary>
		/// 实例化一个对象，使用指定的地址初始化<br />
		/// Instantiate an object, initialize with the specified address
		/// </summary>
		/// <param name="address">传入的地址信息，支持富地址，例如s=2;x=3;100</param>
		// Token: 0x06001E0B RID: 7691 RVA: 0x000904C6 File Offset: 0x0008E6C6
		public ModbusAddress(string address)
		{
			this.Station = -1;
			this.Function = -1;
			base.Address = 0;
			this.Parse(address);
		}

		/// <summary>
		/// 实例化一个对象，使用指定的地址及功能码初始化<br />
		/// Instantiate an object and initialize it with the specified address and function code
		/// </summary>
		/// <param name="address">传入的地址信息，支持富地址，例如s=2;x=3;100</param>
		/// <param name="function">默认的功能码信息</param>
		// Token: 0x06001E0C RID: 7692 RVA: 0x000904F0 File Offset: 0x0008E6F0
		public ModbusAddress(string address, byte function)
		{
			this.Station = -1;
			this.Function = (int)function;
			base.Address = 0;
			this.Parse(address);
		}

		/// <summary>
		/// 实例化一个对象，使用指定的地址，站号，功能码来初始化<br />
		/// Instantiate an object, use the specified address, station number, function code to initialize
		/// </summary>
		/// <param name="address">传入的地址信息，支持富地址，例如s=2;x=3;100</param>
		/// <param name="station">站号信息</param>
		/// <param name="function">默认的功能码信息</param>
		// Token: 0x06001E0D RID: 7693 RVA: 0x0009051A File Offset: 0x0008E71A
		public ModbusAddress(string address, byte station, byte function)
		{
			this.Station = -1;
			this.Function = (int)function;
			this.Station = (int)station;
			base.Address = 0;
			this.Parse(address);
		}

		/// <summary>
		/// 获取或设置当前地址的站号信息<br />
		/// Get or set the station number information of the current address
		/// </summary>
		// Token: 0x1700061F RID: 1567
		// (get) Token: 0x06001E0E RID: 7694 RVA: 0x0009054C File Offset: 0x0008E74C
		// (set) Token: 0x06001E0F RID: 7695 RVA: 0x00090554 File Offset: 0x0008E754
		public int Station { get; set; }

		/// <summary>
		/// 获取或设置当前地址携带的功能码<br />
		/// Get or set the function code carried by the current address
		/// </summary>
		// Token: 0x17000620 RID: 1568
		// (get) Token: 0x06001E10 RID: 7696 RVA: 0x0009055D File Offset: 0x0008E75D
		// (set) Token: 0x06001E11 RID: 7697 RVA: 0x00090565 File Offset: 0x0008E765
		public int Function { get; set; }

		/// <inheritdoc />
		// Token: 0x06001E12 RID: 7698 RVA: 0x00090570 File Offset: 0x0008E770
		public override void Parse(string address)
		{
			bool flag = address.IndexOf(';') < 0;
			if (flag)
			{
				base.Address = ushort.Parse(address);
			}
			else
			{
				string[] array = address.Split(new char[]
				{
					';'
				});
				for (int i = 0; i < array.Length; i++)
				{
					bool flag2 = array[i][0] == 's' || array[i][0] == 'S';
					if (flag2)
					{
						this.Station = (int)byte.Parse(array[i].Substring(2));
					}
					else
					{
						bool flag3 = array[i][0] == 'x' || array[i][0] == 'X';
						if (flag3)
						{
							this.Function = (int)byte.Parse(array[i].Substring(2));
						}
						else
						{
							base.Address = ushort.Parse(array[i]);
						}
					}
				}
			}
		}

		/// <summary>
		/// 地址偏移指定的位置，返回一个新的地址对象<br />
		/// The address is offset by the specified position and a new address object is returned
		/// </summary>
		/// <param name="value">数据值信息</param>
		/// <returns>新增后的地址信息</returns>
		// Token: 0x06001E13 RID: 7699 RVA: 0x00090658 File Offset: 0x0008E858
		public ModbusAddress AddressAdd(int value)
		{
			return new ModbusAddress
			{
				Station = this.Station,
				Function = this.Function,
				Address = (ushort)((int)base.Address + value)
			};
		}

		/// <summary>
		/// 地址偏移1，返回一个新的地址对象<br />
		/// The address is offset by 1 and a new address object is returned
		/// </summary>
		/// <returns>新增后的地址信息</returns>
		// Token: 0x06001E14 RID: 7700 RVA: 0x0009069C File Offset: 0x0008E89C
		public ModbusAddress AddressAdd()
		{
			return this.AddressAdd(1);
		}

		/// <inheritdoc />
		// Token: 0x06001E15 RID: 7701 RVA: 0x000906B8 File Offset: 0x0008E8B8
		public override string ToString()
		{
			StringBuilder stringBuilder = new StringBuilder();
			bool flag = this.Station >= 0;
			if (flag)
			{
				stringBuilder.Append("s=" + this.Station.ToString() + ";");
			}
			bool flag2 = this.Function >= 1;
			if (flag2)
			{
				stringBuilder.Append("x=" + this.Function.ToString() + ";");
			}
			stringBuilder.Append(base.Address.ToString());
			return stringBuilder.ToString();
		}
	}
}
